package com.kye.express.business.popularity;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.webkit.WebView;

import com.kye.express.R;
import com.kye.express.business.base.BaseToolbarActivity;
import com.kye.express.databinding.ActivityWebviewBinding;
import com.kye.express.view.KyeProgressDialog;
import com.kye.express.view.ToastManager;
import com.kye.express.view.webview.FinishJavascriptInterface;
import com.kye.express.view.webview.PopularityJavasriptInterface;
import com.kye.express.view.webview.ToastJavascriptInterface;
import com.kye.model.SharedPreferencesManager;
import com.kye.webview.WebViewFragment;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

import static com.kye.model.SharedPreferencesConfig.KEY_POPULARITY_AGREEMENT;

public class PopularityActivity extends BaseToolbarActivity implements PopularityContract.PopularityView {

    private static final String JS_HOOK_NAME = "hb";

    private ActivityWebviewBinding mBinding;
    private WebViewFragment mWebViewFragment;
    private PopularityContract.PopularityPresenter mPresenter;
    private RxPermissions mRxPermissions;
    private WebView mWebView;
    private Dialog mDialog;

    public static void startActivity(Context context) {
        context.startActivity(
                new Intent(context, PopularityActivity.class)
        );
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_webview);

        initView();
        initEvent();
        initData();
    }

    private void initData() {
        mPresenter = new PopularityContract.PopularityPresenter();
        mPresenter.bind(this);
        mRxPermissions = new RxPermissions(this);
    }

    private void initEvent() {
        mWebViewFragment.setWebViewCallback(new WebViewFragment.WebViewCallback() {
            @Override
            public void callBack(WebView webView) {
                if (webView == null) return;

                mWebView = webView;

                webView.addJavascriptInterface(new ToastJavascriptInterface() {
                    @Override
                    protected Context getContext() {
                        return PopularityActivity.this;
                    }
                }, JS_HOOK_NAME);

                webView.addJavascriptInterface(new FinishJavascriptInterface() {
                    @Override
                    protected void onFinish() {
                        finish();
                    }
                }, JS_HOOK_NAME);

                webView.addJavascriptInterface(new PopularityJavasriptInterface() {
                    @Override
                    protected void onCheckAgreement() {
                        SharedPreferencesManager.commit(KEY_POPULARITY_AGREEMENT, true);
                    }

                    @Override
                    protected boolean onGetCheckAgreementValue() {
                        return SharedPreferencesManager.get(KEY_POPULARITY_AGREEMENT, false);
                    }

                    @Override
                    protected void onLancuherContactSelector() {
                        mRxPermissions.requestEach(Manifest.permission.READ_CONTACTS)
                                .subscribe(new Consumer<Permission>() {
                                    @Override
                                    public void accept(Permission permission) throws Exception {
                                        mPresenter.uploadContacts(getContentResolver());
                                    }
                                });
                    }
                }, JS_HOOK_NAME);
            }
        });
    }

    private void initView() {
        setUpToolbar(mBinding.toolbar);

        mWebViewFragment = WebViewFragment.newInstance("https://res.ky-express.com/activity/hb/", true);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout_container, mWebViewFragment).commit();
    }

    @Override
    public void onLoading() {
        mDialog = KyeProgressDialog.show(this, mDialog);
    }

    @Override
    public void onLoaded() {
        KyeProgressDialog.dismiss(mDialog);
    }

    @Override
    public void onError() {
        ToastManager.show(this, R.string.common_net_error);
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(this, msg);
    }

    @Override
    public void onSuccess() {
        mWebView.loadUrl("javascript:uploadResult(1)");
    }
}
